import axios from 'axios'
import qs from 'querystring'
const service = axios.create({
  baseURL: `${process.env.BASE_API}`, // api的base_url
  timeout: 50000 // request timeout
})
// request interceptor
service.interceptors.request.use(config => {
  if (config.method === 'post') {
    if (!config.data && !config.params) { config.data = {} }
    if (config.data) { config.data = qs.stringify(config.data) }
    if (config.params) { config.data = qs.stringify(config.params) }
    // config.data = qs.stringify(config.params)
  } else {
    if (!config.params) { config.params = {} }
  }
  return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})

// respone interceptor
service.interceptors.response.use(
  response => {
    if (response.data.status === 1) {
      // Indicator.close()
      return response.data.data
    }
  },
  error => {
    console.log('err' + error)// for debug
    return Promise.reject(error)
  })
export default service
